﻿using System;
using System.Collections.Generic;
using WickedFlame.Core;
using WickedFlame.Core.Layers;
using WickedFlame.Core.Shapes;

namespace WickedFlame.Util.Data
{
    [Serializable]
    public class SerializableCanvasContainer
    {
        List<SerializableShapeContainer> layers;

        double width = 0.0;
        public double Width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }

        double height = 0.0;
        public double Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }

        public SerializableCanvasContainer()
        {
        }

        public SerializableCanvasContainer(double width, double height)
        {
            layers = new List<SerializableShapeContainer>();
            this.width = width;
            this.height = height;
        }

        public SerializableCanvasContainer(DrawingCanvas canvas)
        {
            layers = new List<SerializableShapeContainer>();
            width = canvas.Width;
            height = canvas.Height;

            foreach (Layer layer in canvas.Layers)
            {
                var l = new SerializableShapeContainer();
                l.Push(layer);
                layers.Insert(0, l);
                //layers.Add(l);
            }
        }

        public void PushLayer(Layer layer)
        {
            var l = new SerializableShapeContainer();
            l.Push(layer);
            layers.Add(l);
        }

        public void PushLayer(DrawingCanvas canvas)
        {
            layers = new List<SerializableShapeContainer>();
            width = canvas.Width;
            height = canvas.Height;

            foreach (Layer layer in canvas.Layers)
            {
                var l = new SerializableShapeContainer();
                l.Push(layer);
                layers.Add(l);
            }
        }


        public DrawingCanvas GetCanvas()
        {
            if (layers == null)
                throw new Exception("Canvas was not serialized.");

            var canvas = new DrawingCanvas();

            canvas.Height = height;
            canvas.Width = width;

            //int cnt = layers.Count - 1;
            //while (cnt >= 0)
            //{
            //    canvas.AddLayer();
            //    var cont = layers[cnt];
            //    VectorShape shape = cont.PullNext();
            //    while (shape != null)
            //    {
            //        canvas.AddShape(shape);
            //        shape.Invalidate();

            //        shape = cont.PullNext();
            //    }
            //    cnt--;
            //}

            foreach (SerializableShapeContainer cont in layers)
            {
                canvas.AddLayer();

                var shape = cont.PullNext();
                while (shape != null)
                {
                    canvas.AddShape(shape);
                    shape.Invalidate();

                    shape = cont.PullNext();
                }
            }

            // remove all items in the history list
            canvas.ClearHistory();

            return canvas;
        }
        

    }
}
